🎖️GitЯра🎖️
Node / meshtastic / Meshtastic-Android / files / core / ui / src / commonMain / kotlin / org / meshtastic / core / ui / util / ProtoExtensions.kt
Displaying Raw • Download
core/ui/src/commonMain/kotlin/org/meshtastic/core/ui/util/ProtoExtensions.kt 00ad90afdf352e7fddf1d28f3e61a95a763a48dc (00ad90af) Text, 10.54 KB
T8b949e/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787org.meshtastic.core.ui.util
Tff7b72import T7ee787androidx.compose.runtime.Composable
Tff7b72import T7ee787kotlinx.coroutines.flow.first
Tff7b72import T7ee787okio.ByteString
Tff7b72import T7ee787org.jetbrains.compose.resources.stringResource
Tff7b72import T7ee787org.meshtastic.core.common.util.DateFormatter
Tff7b72import T7ee787org.meshtastic.core.common.util.nowMillis
Tff7b72import T7ee787org.meshtastic.core.repository.RadioConfigRepository
Tff7b72import T7ee787org.meshtastic.core.repository.RadioController
Tff7b72import T7ee787org.meshtastic.core.resources.Res
Tff7b72import T7ee787org.meshtastic.core.resources.unknown_age
Tff7b72import T7ee787org.meshtastic.proto.Channel
Tff7b72import T7ee787org.meshtastic.proto.ChannelSet
Tff7b72import T7ee787org.meshtastic.proto.ChannelSettings
Tff7b72import T7ee787org.meshtastic.proto.Config
Tff7b72import T7ee787org.meshtastic.proto.MeshPacket
Tff7b72import T7ee787org.meshtastic.proto.Position
Tff7b72import T7ee787kotlin.time.Duration.Companion.days
Tff7b72import T7ee787org.meshtastic.core.model.Channel Tff7b72as Te6edf3ModelChannel
Tff7b72private Tff7b72const Tff7b72val Te6edf3SECONDS_TO_MILLIS Tff7b72= T79c0ff1T79c0ff0T79c0ff0T79c0ff0L
Tf0883e@Composable
Tff7b72fun Te6edf3PositionTb4b4b4.Td2a8ffformatPositionTimeTb4b4b4(Tb4b4b4)Tb4b4b4: Tffa657String Tb4b4b4{
Tff7b72val Te6edf3currentTime Tff7b72= Te6edf3nowMillis
Tff7b72val Te6edf3sixMonthsAgo Tff7b72= Te6edf3currentTime Tff7b72- T79c0ff1T79c0ff8T79c0ff0.Te6edf3daysTb4b4b4.Te6edf3inWholeMilliseconds
Tff7b72val Te6edf3isOlderThanSixMonths Tff7b72= Te6edf3time Tff7b72* Te6edf3SECONDS_TO_MILLIS Tff7b72< Te6edf3sixMonthsAgo
Tff7b72val Te6edf3timeText Tff7b72=
Tff7b72if Tb4b4b4(Te6edf3isOlderThanSixMonthsTb4b4b4) Tb4b4b4{
Te6edf3stringResourceTb4b4b4(Te6edf3ResTb4b4b4.Te6edf3stringTb4b4b4.Te6edf3unknown_ageTb4b4b4)
Tb4b4b4} Tff7b72else Tb4b4b4{
Te6edf3DateFormatterTb4b4b4.Te6edf3formatDateTimeTb4b4b4(Te6edf3time Tff7b72* Te6edf3SECONDS_TO_MILLISTb4b4b4)
Tb4b4b4}
Tff7b72return Te6edf3timeText
Tb4b4b4}
Tff7b72fun Te6edf3MeshPacketTb4b4b4.Td2a8fftoPositionTb4b4b4(Tb4b4b4)Tb4b4b4: Te6edf3Position? Tb4b4b4{
Tff7b72val Te6edf3decoded Tff7b72= Te6edf3decoded Tff7b72?: Tff7b72return Tff7b72null
Tff7b72return Tff7b72if Tb4b4b4(Te6edf3decodedTb4b4b4.Te6edf3want_response Tff7b72!Tff7b72= Tff7b72trueTb4b4b4) Tb4b4b4{
Te6edf3decodedTb4b4b4.Te6edf3payloadTb4b4b4.Te6edf3let Tb4b4b4{ Te6edf3runCatching Tb4b4b4{ Te6edf3PositionTb4b4b4.Te6edf3ADAPTERTb4b4b4.Te6edf3decodeTb4b4b4(Tffa657itTb4b4b4) Tb4b4b4}Tb4b4b4.Te6edf3getOrNullTb4b4b4(Tb4b4b4) Tb4b4b4}
Tb4b4b4} Tff7b72else Tb4b4b4{
Tff7b72null
Tb4b4b4}
Tb4b4b4}
T8b949e/**
* Builds a [Channel] list from the difference between two [ChannelSettings] lists. Only changes are included in the
* resulting list.
*
* @param new The updated [ChannelSettings] list.
* @param old The current [ChannelSettings] list (required when disabling unused channels).
* @return A [Channel] list containing only the modified channels.
*/
Tff7b72fun Td2a8ffgetChannelListTb4b4b4(Te6edf3newTb4b4b4: Te6edf3ListTff7b72<Te6edf3ChannelSettingsTff7b72>Tb4b4b4, Te6edf3oldTb4b4b4: Te6edf3ListTff7b72<Te6edf3ChannelSettingsTff7b72>Tb4b4b4)Tb4b4b4: Te6edf3ListTff7b72<Te6edf3ChannelTff7b72> Tff7b72= Te6edf3buildList Tb4b4b4{
Tff7b72for Tb4b4b4(Te6edf3i Tff7b72in T79c0ff0.Tb4b4b4.Te6edf3maxOfTb4b4b4(Te6edf3oldTb4b4b4.Te6edf3lastIndexTb4b4b4, Te6edf3newTb4b4b4.Te6edf3lastIndexTb4b4b4)Tb4b4b4) Tb4b4b4{
Tff7b72if Tb4b4b4(Te6edf3oldTb4b4b4.Te6edf3getOrNullTb4b4b4(Te6edf3iTb4b4b4) Tff7b72!Tff7b72= Te6edf3newTb4b4b4.Te6edf3getOrNullTb4b4b4(Te6edf3iTb4b4b4)Tb4b4b4) Tb4b4b4{
Te6edf3addTb4b4b4(
Te6edf3ChannelTb4b4b4(
Te6edf3role Tff7b72=
Tff7b72when Tb4b4b4(Te6edf3iTb4b4b4) Tb4b4b4{
T79c0ff0 Tff7b72-Tff7b72> Te6edf3ChannelTb4b4b4.Te6edf3RoleTb4b4b4.Te6edf3PRIMARY
Tff7b72in T79c0ff1.Tb4b4b4.Te6edf3newTb4b4b4.Te6edf3lastIndex Tff7b72-Tff7b72> Te6edf3ChannelTb4b4b4.Te6edf3RoleTb4b4b4.Te6edf3SECONDARY
Tff7b72else Tff7b72-Tff7b72> Te6edf3ChannelTb4b4b4.Te6edf3RoleTb4b4b4.Te6edf3DISABLED
Tb4b4b4}Tb4b4b4,
Te6edf3index Tff7b72= Te6edf3iTb4b4b4,
Te6edf3settings Tff7b72= Te6edf3newTb4b4b4.Te6edf3getOrNullTb4b4b4(Te6edf3iTb4b4b4) Tff7b72?: Te6edf3ChannelSettingsTb4b4b4(Tb4b4b4)Tb4b4b4,
Tb4b4b4)Tb4b4b4,
Tb4b4b4)
Tb4b4b4}
Tb4b4b4}
Tb4b4b4}
T8b949e/**
* Builds an authoritative [Channel] list for a full REPLACE import. Every position in [new] is emitted (PRIMARY at
* index 0, SECONDARY for 1..new.lastIndex) and any trailing positions beyond [new]'s range are emitted as DISABLED so
* the radio stops using them.
*
* Unlike [getChannelList], this does NOT skip positions where `currentSettings[i] == new[i]`: the imported set is
* authoritative, the local cache must not gate the writes, and silent diff-skips during REPLACE were the source of
* stale channels.
*
* [currentSettings] is consulted only for its size (to determine trailing DISABLED writes); its values are never
* compared against [new]. Callers should read it from `radioConfigRepository.channelSetFlow.first().settings`, not from
* a `stateInWhileSubscribed` StateFlow's `.value` — the StateFlow placeholder window can return an empty list and
* suppress trailing DISABLED writes.
*
* Edge case: if [new] is empty, every emitted slot (including index 0) is DISABLED rather than wrongly promoting an
* empty [ChannelSettings] to PRIMARY.
*
* @param new The imported [ChannelSettings] list. Every index becomes a write to the radio.
* @param currentSettings The current [ChannelSettings] list. Only its size is used; trailing indices past [new] become
* DISABLED writes so leftover slots are cleared.
* @return A [Channel] list covering every slot the radio needs written to materialize [new] and clear leftover slots.
*/
Tff7b72fun Td2a8ffgetChannelReplacementListTb4b4b4(Te6edf3newTb4b4b4: Te6edf3ListTff7b72<Te6edf3ChannelSettingsTff7b72>Tb4b4b4, Te6edf3currentSettingsTb4b4b4: Te6edf3ListTff7b72<Te6edf3ChannelSettingsTff7b72>Tb4b4b4)Tb4b4b4: Te6edf3ListTff7b72<Te6edf3ChannelTff7b72> Tff7b72=
Te6edf3buildList Tb4b4b4{
Tff7b72for Tb4b4b4(Te6edf3i Tff7b72in T79c0ff0.Tb4b4b4.Te6edf3maxOfTb4b4b4(Te6edf3currentSettingsTb4b4b4.Te6edf3lastIndexTb4b4b4, Te6edf3newTb4b4b4.Te6edf3lastIndexTb4b4b4)Tb4b4b4) Tb4b4b4{
Te6edf3addTb4b4b4(
Te6edf3ChannelTb4b4b4(
Te6edf3role Tff7b72=
Tff7b72when Tb4b4b4(Te6edf3iTb4b4b4) Tb4b4b4{
T8b949e// Empty-new is a degenerate import: every slot (including 0) must be DISABLED.
T79c0ff0 Tff7b72-Tff7b72> Tff7b72if Tb4b4b4(Te6edf3newTb4b4b4.Te6edf3isEmptyTb4b4b4(Tb4b4b4)Tb4b4b4) Te6edf3ChannelTb4b4b4.Te6edf3RoleTb4b4b4.Te6edf3DISABLED Tff7b72else Te6edf3ChannelTb4b4b4.Te6edf3RoleTb4b4b4.Te6edf3PRIMARY
Tff7b72in T79c0ff1.Tb4b4b4.Te6edf3newTb4b4b4.Te6edf3lastIndex Tff7b72-Tff7b72> Te6edf3ChannelTb4b4b4.Te6edf3RoleTb4b4b4.Te6edf3SECONDARY
Tff7b72else Tff7b72-Tff7b72> Te6edf3ChannelTb4b4b4.Te6edf3RoleTb4b4b4.Te6edf3DISABLED
Tb4b4b4}Tb4b4b4,
Te6edf3index Tff7b72= Te6edf3iTb4b4b4,
Te6edf3settings Tff7b72= Te6edf3newTb4b4b4.Te6edf3getOrNullTb4b4b4(Te6edf3iTb4b4b4) Tff7b72?: Te6edf3ChannelSettingsTb4b4b4(Tb4b4b4)Tb4b4b4,
Tb4b4b4)Tb4b4b4,
Tb4b4b4)
Tb4b4b4}
Tb4b4b4}
T8b949e/**
* Applies an imported [ChannelSet] as an authoritative replacement to the radio and local cache.
*
* Reads the current channel set from [radioConfigRepository]'s flow (avoiding the StateFlow placeholder window), builds
* the authoritative replacement list via [getChannelReplacementList], enqueues each channel write to the radio
* sequentially via [radioController], then atomically replaces the local cached settings.
*
* setLocalChannel returns once the packet is enqueued, not after firmware ACK — firmware echoes via
* MeshConfigHandlerImpl can still arrive after [radioConfigRepository.replaceAllSettings] and are tracked separately.
*
* Does NOT handle LoRa config — callers are responsible for comparing and sending `lora_config` if present.
*
* @param channelSet The imported [ChannelSet] to apply as a replacement.
* @param radioController The [RadioController] used to enqueue channel writes.
* @param radioConfigRepository The [RadioConfigRepository] providing the current channel flow and cache.
*/
Tff7b72suspend Tff7b72fun Td2a8ffapplyReplacementChannelSetTb4b4b4(
Te6edf3channelSetTb4b4b4: Te6edf3ChannelSetTb4b4b4,
Te6edf3radioControllerTb4b4b4: Te6edf3RadioControllerTb4b4b4,
Te6edf3radioConfigRepositoryTb4b4b4: Te6edf3RadioConfigRepositoryTb4b4b4,
Tb4b4b4) Tb4b4b4{
Tff7b72val Te6edf3currentSettings Tff7b72= Te6edf3radioConfigRepositoryTb4b4b4.Te6edf3channelSetFlowTb4b4b4.Te6edf3firstTb4b4b4(Tb4b4b4)Tb4b4b4.Te6edf3settings
Tff7b72for Tb4b4b4(Te6edf3channel Tff7b72in Te6edf3getChannelReplacementListTb4b4b4(Te6edf3channelSetTb4b4b4.Te6edf3settingsTb4b4b4, Te6edf3currentSettingsTb4b4b4)Tb4b4b4) Tb4b4b4{
Te6edf3radioControllerTb4b4b4.Te6edf3setLocalChannelTb4b4b4(Te6edf3channelTb4b4b4)
Tb4b4b4}
Te6edf3radioConfigRepositoryTb4b4b4.Te6edf3replaceAllSettingsTb4b4b4(Te6edf3channelSetTb4b4b4.Te6edf3settingsTb4b4b4)
Tb4b4b4}
T8b949e/**
* Builds the filtered ADD-mode preview for QR import: existing channels followed by only the unique incoming channels.
*
* Incoming channels that are semantic duplicates (same effective name + effective PSK) of an existing or earlier
* incoming channel are omitted entirely from the preview — they are not shown to the user. Unique incoming channels are
* appended in scanned order and selected by default while firmware channel capacity remains; unique channels beyond
* [maxChannels] stay visible but unchecked.
*
* Semantic identity is resolved via the [Channel] domain model so preset/default channels match correctly across modem
* presets: empty names resolve to the preset display name, and 1-byte PSK markers expand to the full default key.
*
* @param existing The current [ChannelSettings] list on the radio. Always shown, always selected.
* @param incoming The imported [ChannelSettings] list. Duplicates omitted; uniques appended in order.
* @param loraConfig The current [Config.LoRaConfig], used to resolve effective channel identity.
* @param maxChannels Firmware channel limit. Unique incoming selections stop when this is reached.
* @return A [ChannelAddPreview] whose [settings] and [selections] are aligned and size-matched.
*/
Tff7b72fun Td2a8ffgetChannelPreviewForAddTb4b4b4(
Te6edf3existingTb4b4b4: Te6edf3ListTff7b72<Te6edf3ChannelSettingsTff7b72>Tb4b4b4,
Te6edf3incomingTb4b4b4: Te6edf3ListTff7b72<Te6edf3ChannelSettingsTff7b72>Tb4b4b4,
Te6edf3loraConfigTb4b4b4: Te6edf3ConfigTb4b4b4.Te6edf3LoRaConfigTb4b4b4,
Te6edf3maxChannelsTb4b4b4: Tffa657IntTb4b4b4,
Tb4b4b4)Tb4b4b4: Te6edf3ChannelAddPreview Tb4b4b4{
Tff7b72val Te6edf3seen Tff7b72= Te6edf3existingTb4b4b4.Te6edf3map Tb4b4b4{ Tffa657itTb4b4b4.Te6edf3channelIdentityTb4b4b4(Te6edf3loraConfigTb4b4b4) Tb4b4b4}Tb4b4b4.Te6edf3toMutableSetTb4b4b4(Tb4b4b4)
Tff7b72val Te6edf3previewSettings Tff7b72= Te6edf3existingTb4b4b4.Te6edf3toMutableListTb4b4b4(Tb4b4b4)
Tff7b72val Te6edf3previewSelections Tff7b72= Te6edf3MutableListTb4b4b4(Te6edf3existingTb4b4b4.Te6edf3sizeTb4b4b4) Tb4b4b4{ Tff7b72true Tb4b4b4}
Tff7b72var Te6edf3remaining Tff7b72= Tb4b4b4(Te6edf3maxChannels Tff7b72- Te6edf3existingTb4b4b4.Te6edf3sizeTb4b4b4)Tb4b4b4.Te6edf3coerceAtLeastTb4b4b4(T79c0ff0Tb4b4b4)
Tff7b72for Tb4b4b4(Te6edf3channel Tff7b72in Te6edf3incomingTb4b4b4) Tb4b4b4{
Tff7b72val Te6edf3shouldShow Tff7b72= Tff7b72!Te6edf3channelTb4b4b4.Te6edf3isPlaceholderTb4b4b4(Tb4b4b4)
Tff7b72val Te6edf3identity Tff7b72= Tff7b72if Tb4b4b4(Te6edf3shouldShowTb4b4b4) Te6edf3channelTb4b4b4.Te6edf3channelIdentityTb4b4b4(Te6edf3loraConfigTb4b4b4) Tff7b72else Tff7b72null
T8b949e// Omit blank placeholders and semantic duplicates entirely — they are not shown to the user.
Tff7b72if Tb4b4b4(Te6edf3identity Tff7b72!Tff7b72= Tff7b72null Tff7b72&Tff7b72& Te6edf3seenTb4b4b4.Te6edf3addTb4b4b4(Te6edf3identityTb4b4b4)Tb4b4b4) Tb4b4b4{
Te6edf3previewSettings Tff7b72+Tff7b72= Te6edf3channel
Tff7b72val Te6edf3shouldSelect Tff7b72= Te6edf3remaining Tff7b72> T79c0ff0
Te6edf3previewSelections Tff7b72+Tff7b72= Te6edf3shouldSelect
Tff7b72if Tb4b4b4(Te6edf3shouldSelectTb4b4b4) Te6edf3remainingTff7b72-Tff7b72-
Tb4b4b4}
Tb4b4b4}
Tff7b72return Te6edf3ChannelAddPreviewTb4b4b4(Te6edf3settings Tff7b72= Te6edf3previewSettingsTb4b4b4, Te6edf3selections Tff7b72= Te6edf3previewSelectionsTb4b4b4)
Tb4b4b4}
T8b949e/** Filtered ADD-mode preview: the visible channel list paired with its default selections (always size-matched). */
Tff7b72data Tff7b72class T56d364ChannelAddPreviewTb4b4b4(Tff7b72val Te6edf3settingsTb4b4b4: Te6edf3ListTff7b72<Te6edf3ChannelSettingsTff7b72>Tb4b4b4, Tff7b72val Te6edf3selectionsTb4b4b4: Te6edf3ListTff7b72<Tffa657BooleanTff7b72>Tb4b4b4)
T8b949e/** Semantic channel identity based on effective name and effective PSK. */
Tff7b72private Tff7b72data Tff7b72class T56d364ChannelIdentityTb4b4b4(Tff7b72val Te6edf3nameTb4b4b4: Tffa657StringTb4b4b4, Tff7b72val Te6edf3pskTb4b4b4: Te6edf3ByteStringTb4b4b4) Tb4b4b4{
T8b949e// Redact the effective PSK from auto-generated diagnostics so a cryptographic key never leaks
T8b949e// via toString() in exception messages, debug logs, or stack traces.
Tff7b72override Tff7b72fun Td2a8fftoStringTb4b4b4(Tb4b4b4)Tb4b4b4: Tffa657String Tff7b72= Ta5d6ff"Ta5d6ffChannelIdentity(name=Tffd700$Te6edf3nameTa5d6ff, psk=<redacted>)Ta5d6ff"
Tb4b4b4}
T8b949e/** True when a [ChannelSettings] carries no name and no PSK — a placeholder, not an intended channel. */
Tff7b72private Tff7b72fun Te6edf3ChannelSettingsTb4b4b4.Td2a8ffisPlaceholderTb4b4b4(Tb4b4b4)Tb4b4b4: Tffa657Boolean Tff7b72= Te6edf3nameTb4b4b4.Te6edf3isNullOrBlankTb4b4b4(Tb4b4b4) Tff7b72&Tff7b72& Tb4b4b4(Te6edf3psk Tff7b72=Tff7b72= Tff7b72null Tff7b72|Tff7b72| Te6edf3pskTb4b4b4.Te6edf3size Tff7b72=Tff7b72= T79c0ff0Tb4b4b4)
T8b949e/** Resolves the [ChannelIdentity] of this [ChannelSettings] under the given [Config.LoRaConfig]. */
Tff7b72private Tff7b72fun Te6edf3ChannelSettingsTb4b4b4.Td2a8ffchannelIdentityTb4b4b4(Te6edf3loraConfigTb4b4b4: Te6edf3ConfigTb4b4b4.Te6edf3LoRaConfigTb4b4b4)Tb4b4b4: Te6edf3ChannelIdentity Tb4b4b4{
Tff7b72val Te6edf3channel Tff7b72= Te6edf3ModelChannelTb4b4b4(Te6edf3settings Tff7b72= Tff7b72thisTb4b4b4, Te6edf3loraConfig Tff7b72= Te6edf3loraConfigTb4b4b4)
Tff7b72return Te6edf3ChannelIdentityTb4b4b4(Te6edf3name Tff7b72= Te6edf3channelTb4b4b4.Te6edf3nameTb4b4b4, Te6edf3psk Tff7b72= Te6edf3channelTb4b4b4.Te6edf3pskTb4b4b4)
Tb4b4b4}
Served by rngit 1.5.2 - Generated in 0.11s